using System;
using System.Collections.Generic;
using System.Text;
using Coversant.SoapBox.Server.ServerCore.Security;

namespace WeavverLib.SoapBox
{
     [Coversant.Trace.Traceable(Coversant.Trace.TraceLevel.Warning)]
     public sealed class DatabaseUserManagerConfig : UserManagerConfig
     {
         public static Type   MyType        = typeof(DatabaseUserManagerConfig);
         private const string ASSEMBLY_NAME = "DrupalUserManager.dll";
         public string dbtype = "mysql";
         public string dbname = "";
         public string dbhost = "";
         public string dbuser = "";
         public string dbpass = "";
//--------------------------------------------------------------------------------------------
         public DatabaseUserManagerConfig() : base(ASSEMBLY_NAME, typeof(DatabaseUserManagerConfig).FullName)
         {
             this.SupportedSASLMechanisms.Add("PLAIN");
             //this.SupportedSASLMechanisms.Add("DIGEST-MD5");
             this.SupportsNonSASLDigestAuth = false;
             this.SupportsNonSASLPlainAuth = false;
         }
//--------------------------------------------------------------------------------------------
          public override void GetProperties(System.Xml.XmlElement xml)
          {
               base.GetProperties(xml);
               xml.SetAttribute("dbtype", dbtype);
               xml.SetAttribute("dbname", dbname);
               xml.SetAttribute("dbhost", dbhost);
               xml.SetAttribute("dbuser", dbuser);
               xml.SetAttribute("dbpass", dbpass);
          }
//--------------------------------------------------------------------------------------------
          public override void SetProperties(System.Xml.XmlElement xml)
          {
               base.SetProperties(xml);
               if (Coversant.Trace.WTrace.TraceEnabled)
                    Coversant.Trace.WTrace.TraceWarning("Drupal XML Config: " + xml.ToString(), MyType);

               string log = "";
               foreach (System.Xml.XmlElement element in xml.ChildNodes)
               {
                    switch (element.Name)
                    {
                         case "type":
                              dbtype = element.InnerText;
                              break;

                         case "host":
                              dbhost = element.InnerText;
                              break;

                         case "database":
                              dbname = element.InnerText;
                              break;

                         case "user":
                              dbuser = element.InnerText;
                              break;

                         case "pass":
                              dbpass = element.InnerText;
                              break;
                    }
                    log += "elem: " + element.Name + " - val: " + element.InnerText + "\r\n";
               }
               System.IO.File.WriteAllText("C:\\out.log", xml.OuterXml + "\r\n" + xml.InnerXml + "\r\n\r\n" + log);
          }
//--------------------------------------------------------------------------------------------
         protected override UserManager CreateConcreateUserManagerInstance()
         {
             return new DatabaseUserManager(this);
         }
//--------------------------------------------------------------------------------------------
         public override bool SupportsRoleManagement
         {
             get { return false; }
         }
//--------------------------------------------------------------------------------------------
         public override bool SupportsRoleEnumeration
         {
             get { return true; }
         }
//--------------------------------------------------------------------------------------------
         public override bool SupportsUserManagement
         {
             get { return false; }
         }
//--------------------------------------------------------------------------------------------
     }
}
